package com.note.simpleshop.module.user.auth;
import com.note.simpleshop.entity.TbToken;
import com.note.simpleshop.entity.TbUser;
import com.note.simpleshop.module.user.service.UserService;
import com.note.simpleshop.utils.SpringContextUtils;
import com.note.simpleshop.utils.WebUtil;
import org.apache.commons.lang3.StringUtils;
public class UserUtils
{
    /**
     *
     * 获取当前登录用户的信息
     * @return
     */
    public static TbUser currentUsers()
    {
        TokenManager tokenManager=(TokenManager) SpringContextUtils.getContext().getBean("mysqlTokenManager");
        UserService userService=(UserService) SpringContextUtils.getContext().getBean("userService");
        String token= WebUtil.getHeader("token");
        //判断token
        if(StringUtils.isNotEmpty(token))
        {
            TbToken tokenModel=(TbToken) tokenManager.getObject(token);
            return userService.findById(tokenModel.getUserId());
        }
        return null;
    }
    public static String currentUserId()
    {
        TbUser users=currentUsers();
        if(users==null)
        {
            return null;
        }
        return users.getId();

    }
}
